package tw.edu.tku.im.maze;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

public class UpdateRoomRep extends RepPacket {

   boolean roomSurvive;
   String roomName;
   int mapLv;
   Member[] team1;
   Member[] team2;
   boolean start;
   MazeMap map;
   
   //-------------------------------------------------------
   public UpdateRoomRep(boolean roomSurvive, String roomName, int mapLv,
                        Member[] team1, boolean start, MazeMap map) 
   {
      this(roomSurvive, roomName, mapLv, team1, null, start, map);
   }
   
   public UpdateRoomRep(boolean roomSurvive, String roomName, int mapLv, 
               Member[] team1, Member[] team2, boolean start, MazeMap map) 
   {
      this.roomSurvive = roomSurvive;
      this.roomName = roomName;
      this.mapLv = mapLv;
      this.team1 = team1;
      this.team2 = team2;
      this.start = start;
      this.map = map;
   }
   
   //-------------------------------------------------------
   public void setRoomSurvive(boolean roomSurvive) {
      this.roomSurvive= roomSurvive;
   }
   public boolean getRoomSurvive() {
      return this.roomSurvive;
   }
   
   //-------------------------------------------------------
   public String getRoomName() {
      return roomName;
   }
   public void setRoomName(String roomName) {
      this.roomName = roomName;
   }

   //-------------------------------------------------------
   public int getMapLv() {
      return mapLv;
   }
   public void setMapLv(int mapLv) {
      this.mapLv = mapLv;
   }
   //-------------------------------------------------------
   public void setTeam1(Member[] team) {
      this.team1 = team;
   }
   public Member[] getTeam1() {
      return this.team1;
   }
   
   //-------------------------------------------------------
   public void setTeam2(Member[] team) {
      this.team2 = team;
   }
   public Member[] getTeam2() {
      return this.team2;
   }
   
   
   //-------------------------------------------------------
   public void setStart(boolean start) {
      this.start= start;
   }
   public boolean getStart() {
      return this.start;
   }
   
   //-------------------------------------------------------
   public void setMap(MazeMap map) {
      this.map = map;
   }
   public MazeMap getMap() {
      return this.map;
   }
   
   //-------------------------------------------------------   
   public String toJSONString() throws JSONException {
      JSONArray team1 = new JSONArray();
      JSONObject result = new JSONObject();
      result.put("roomSurvive", this.roomSurvive);
      result.put("roomName", this.roomName);
      result.put("mapLv", this.mapLv);
      if(this.team1!=null) {
         for(Member member : this.team1) {
            if(member==null) { break; }
            team1.put(new JSONObject(member.toJSONString()));
         }
         if(this.team1[0]!=null) {
            result.put("team1", team1); 
         }
      }
      if(this.team2!=null) {
         JSONArray team2 = new JSONArray();
         for(Member member : this.team2) {
            if(member==null) { break; }
            team2.put(new JSONObject(member.toJSONString()));
         }
         if(this.team2[0]!=null) {
            result.put("team2", team2); 
         }
      }
      result.put("start", this.start);
      if(this.map!=null) {
         result.put("map", new JSONObject(this.map.toJSONString())); 
      }
      
      JSONObject jsonObj = new JSONObject();
      jsonObj.put("result", result);
      jsonObj.put("action", "updateRoomResponse");
      return jsonObj.toString();
   }

   @Override
   public String getPacketName() {
      return "updateRoomResponse";
   }

   @Override
   public int describeContents() {
      return 0;
   }

   @Override
   public void writeToParcel(Parcel arg0, int arg1) {
      
   }

}
